package com.atjava.webspider.logic;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class CommonLogic {

	public static Named getByName(List<? extends Named> list, String name){
		//pattern
		for(int i=0;i<list.size();i++){
			Named nmd = list.get(i);
			if(name.equals(nmd.getName()))
				return nmd;
		}
		return null;
	}
	
	public static Named removeByName(List<? extends Named> list, String name){
		int index = getIndexByName(list, name);
		return list.remove(index);
	}
	
	public static int getIndexByName(List<? extends Named> list, String name){
		for (int i = 0; i < list.size(); i++) {
			Named nmd = list.get(i);
			if(nmd.getName().equals(name)){
				return i;
			}
		}
		return -1;
	}
	
	public static Set<String> getCrossElements(Set<String> set1,Set<String> set2){
		Set<String> crossSet = new HashSet<String>();
		Set<String> smlSet = set1.size() > set2.size() ? set2 : set1;
		Iterator<String> smlIter = smlSet.iterator();
		while(smlIter.hasNext()){
			String e = smlIter.next();
			if(set1.contains(e) && set2.contains(e))
				crossSet.add(e);
		}
		return crossSet;
	}
	
	public static ValuePrcsor getDbValueProcsor(){
		return new MySqlValuePrcsor();
	}

}
